package com.ruoyi.web.controller.module.examine;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.estate.Estate;
import com.ruoyi.system.domain.module.property.Property;
import com.ruoyi.system.domain.module.examine.ExamineApprove;
import com.ruoyi.system.domain.module.examine.ExamineApproveTemplate;
import com.ruoyi.system.service.module.estate.IEstateService;
import com.ruoyi.system.service.module.property.IPropertyService;
import com.ruoyi.system.service.module.examine.IExamineApproveService;
import com.ruoyi.system.service.module.examine.IExamineApproveTemplateService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 审批类目Controller
 *
 * @author ruoyi
 * @date 2022-11-23
 */
@Validated
@Api(value = "审批类目控制器", tags = {"审批类目管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/approve")
public class ExamineApproveController extends BaseController {

    private final IExamineApproveService examineApproveService;
    private final IPropertyService propertyService;
    private final IEstateService estateService;
    private final IExamineApproveTemplateService examineApproveTemplateService;
    /**
     * 查询审批类目列表
     */
    @ApiOperation("查询审批类目列表")
    @PreAuthorize("@ss.hasPermi('module:approve:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamineApprove> list(@Validated ExamineApprove examineApprove) {
		SysUser sysUser = getUser();
        QueryWrapper<ExamineApprove> qw = new QueryWrapper<>();
        if(examineApprove.getPropertyId()!=null){
            qw.eq("property_id",examineApprove.getPropertyId());
        }
		if(sysUser.getUserType() == 2){
			qw.eq("property_id",sysUser.getPropertyId());
			if (sysUser.getEstateId()!=null){
				qw.eq("estate_id",sysUser.getEstateId());
			}
		} else if(sysUser.getUserType() == 3){
			qw.eq("estate_id",sysUser.getEstateId());
		}
        TableDataInfo<ExamineApprove> td = PageUtils.buildDataInfo(examineApproveService.page(PageUtils.buildPage(),qw));
        List<ExamineApprove> list = td.getRows();
        for (ExamineApprove list1:list){
            if (list1.getTemplateId()!=null){
                ExamineApproveTemplate template=examineApproveTemplateService.findById(list1.getTemplateId());
                if (template!=null){
                    list1.setTemplateName(template.getTemplateName());
                }
            }
        }
        return td;
    }

    /**
     * 导出审批类目列表
     */
    @ApiOperation("导出审批类目列表")
    @PreAuthorize("@ss.hasPermi('module:approve:export')")
    @Log(title = "审批类目", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<ExamineApprove> export(@Validated ExamineApprove examineApprove) {
        List<ExamineApprove> list = examineApproveService.list();
        ExcelUtil<ExamineApprove> util = new ExcelUtil<ExamineApprove>(ExamineApprove.class);
        return util.exportExcel(list, "审批类目");
    }

    /**
     * 获取审批类目详细信息
     */
    @ApiOperation("获取审批类目详细信息")
    @PreAuthorize("@ss.hasPermi('module:approve:query')")
    @GetMapping("/{id}")
    public AjaxResult<ExamineApprove> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        ExamineApprove examineApprove = examineApproveService.getById(id);
        return AjaxResult.success(examineApprove);
    }

    /**
     * 新增审批类目
     */
    @ApiOperation("新增审批类目")
    @PreAuthorize("@ss.hasPermi('module:approve:add')")
    @Log(title = "审批类目", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody ExamineApprove examineApprove) {
        if (examineApprove.getPropertyId()!=null){
            Property property=propertyService.findById(examineApprove.getPropertyId());
            if (property!=null){
                examineApprove.setPropertyName(property.getPropertyName());
            }
        }
        if (examineApprove.getEstateId()!=null){
            Estate estate=estateService.findById(examineApprove.getEstateId());
            if (estate!=null){
                examineApprove.setEstateName(estate.getEstateName());
            }
        }
        return toAjax(examineApproveService.save(examineApprove) ? 1 : 0);
    }

    /**
     * 修改审批类目
     */
    @ApiOperation("修改审批类目")
    @PreAuthorize("@ss.hasPermi('module:approve:edit')")
    @Log(title = "审批类目", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody ExamineApprove examineApprove) {
        return toAjax(examineApproveService.updateById(examineApprove) ? 1 : 0);
    }

    /**
     * 删除审批类目
     */
    @ApiOperation("删除审批类目")
    @PreAuthorize("@ss.hasPermi('module:approve:remove')")
    @Log(title = "审批类目" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(examineApproveService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
